package com.example.android.courtconter;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int scoreTeamA = 0;
    int scoreTeamB = 0;
    EditText editText = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void APoint3(View view) {
        scoreTeamA += 3;
        displayForTeamA(scoreTeamA);
        closeInputMethod();
    }

    public void APoint2(View view) {
        scoreTeamA += 2;
        displayForTeamA(scoreTeamA);
        closeInputMethod();
    }

    public void APoint1(View view) {
        scoreTeamA += 1;
        displayForTeamA(scoreTeamA);
        closeInputMethod();
    }


    public void APoint0(View view) {
        if (scoreTeamA > 0) {
            scoreTeamA -= 1;
            displayForTeamA(scoreTeamA);
        } else {
            Toast.makeText(MainActivity.this, "分数不能小于0", Toast.LENGTH_SHORT).show();
        }
        closeInputMethod();
    }
    public void BPoint3(View view) {
        scoreTeamB += 3;
        displayForTeamB(scoreTeamB);
        closeInputMethod();
    }

    public void BPoint2(View view) {
        scoreTeamB += 2;
        displayForTeamB(scoreTeamB);
        closeInputMethod();
    }

    public void BPoint1(View view) {
        scoreTeamB += 1;
        displayForTeamB(scoreTeamB);
        closeInputMethod();
    }

    public void BPoint0(View view) {
        if (scoreTeamB > 0) {
            scoreTeamB -= 1;
            displayForTeamB(scoreTeamB);
        } else {
            Toast.makeText(MainActivity.this, "分数不能小于0", Toast.LENGTH_SHORT).show();
        }
        closeInputMethod();
    }

    public void resetScore(View view) {
        scoreTeamA = 0;
        scoreTeamB = 0;
        displayForTeamA(scoreTeamA);
        displayForTeamB(scoreTeamB);
    }

    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }

    private void closeInputMethod() {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    
}
